package com.Lgwd.service.backService.backServiceImpl;

import com.Lgwd.dao.BackDao;
import com.Lgwd.dao.ItemDao;
import com.Lgwd.dao.UserDao;
import com.Lgwd.entity.BackInfoEntity;
import com.Lgwd.entity.UserEntiy;
import com.Lgwd.entity.UserItemInfoEntity;
import com.Lgwd.repository.BackInfoRepository;
import com.Lgwd.repository.UserRepository;
import com.Lgwd.service.backService.BackInfoService;
import com.Lgwd.service.backService.UserItemInfoService;
import com.Lgwd.service.userService.UserService;
import com.Lgwd.utils.RandomUtils;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;

/**
 * 背包实现类
 */
@Slf4j
@Service
public class BackInfoServiceImpl implements BackInfoService {

    @Autowired
    BackInfoRepository backInfoRepository;
    @Autowired
    UserItemInfoService userItemInfoService;
    @Autowired
    UserService userService;
    @Autowired
    UserRepository userRepository;

    /**
     * 掉落物添加都背包
     */

    @Override
    public void addItemToUserBack(ItemDao itemDao, String user_account, String item_id) {
        BackInfoEntity backInfoEntity = new BackInfoEntity();
        backInfoEntity.setUser_account(user_account);
        backInfoEntity.setFlag(1);
        backInfoEntity.setItem_name(itemDao.getItem_name());
        backInfoEntity.setItem_quality(itemDao.getProperty_quality());
        //装备唯一标识
        backInfoEntity.setItem_id(item_id);
        backInfoEntity.setMoral_type_id(itemDao.getMoral_type_id());
        backInfoEntity.setItem_profession(itemDao.getProfession());
        backInfoEntity.setEquip(0);
        backInfoEntity.setItem_code_id(itemDao.getItem_code_id());
        backInfoRepository.save(backInfoEntity);
    }


    /**
     * 查看背包物品 ==前台点击打开背包后调用
     * 根据账号   和 flag字段查询
     * 返回所有字段
     * 关键字段item_id
     * moral_type_id装备类型ID代表每个部位只能装备一个类型
     * equip是否已穿戴 默认0
     * item_profession 所属职业 1战士 2法师  0其他道具
     */
    @Override
    public List<BackInfoEntity> findAllByUserAccountAndFlag(BackDao backDao) {
        List<BackInfoEntity> backInfoEntities = backInfoRepository.findByUser_accountAndFlag(backDao.getUser_account(), 1);
        return backInfoEntities;
    }

    /**
     * 用户操作 针对于装备  有 穿戴  卸下  删除
     * <p>
     * 穿戴该装备时 更新user表对应数据
     * 根据item_id获取到 装备的所有属性
     * 根据传入账号获取到user对应的属性
     * 穿戴前判断是否已装备同类型装备  已装备进行替换  没有装备则进行装备  根据equip,item_profession,moral_type_id判断是否装备
     * 判断是否是该职业武器
     * 设置完所有信息
     * 最终返回user数据  更新背包物品数据
     * <p>
     * 卸下操作
     * user表数据-装备属性 进行更新  武器可装备状态进行更新
     * 删除操作
     * 物理删除根据item_id,flag字段删除backinfo表装备  与表关联的数据表useriteminfo
     * 删除时候优先判断是否已穿戴
     */
    @Override
    public UserEntiy updateUserPropertyAndUseItem(BackDao backDao) {
        //根据item_id查询装备属性
        UserItemInfoEntity item_info = userItemInfoService.findItemInfoByItemId(backDao);
        //根据item_id修改背包里equip字段为1 表示穿戴
        BackInfoEntity newBack = backInfoRepository.findByItem_idAAndFlag(item_info.getItem_id(), 1);
      //判断是否是同类型  如果是 则进行替换 查询出所有同类型装备
        List<BackInfoEntity> oldBack = backInfoRepository.findByItem_code_id(item_info.getItem_code_id(), 1, backDao.getUser_account());
        oldBack.forEach(backInfoEntity -> {
            if (backInfoEntity.getEquip() == 1) {
                log.info("同类型装备替换");
                backInfoEntity.setEquip(0);
                backInfoRepository.saveAndFlush(backInfoEntity);
                UserItemInfoEntity userItemInfoEntity = userItemInfoService.findItemInfoByItemIdString(backInfoEntity.getItem_id());
                //根据账号查询信息进行计算
                UserEntiy userEntiy = userRepository.findByUser_account(backDao.getUser_account());
                log.info("替换前的信息为：" + userEntiy);
                userEntiy = updateUserInfoJ(userItemInfoEntity, userEntiy);
                log.info("替换后的信息为：" + userEntiy);
                userRepository.saveAndFlush(userEntiy);
                return;
            }
        });

        newBack.setEquip(1);
        backInfoRepository.saveAndFlush(newBack);

        //根据账号查询信息进行计算
        UserEntiy userEntiy = userRepository.findByUser_account(backDao.getUser_account());
        log.info("获得的账号信息为：" + userEntiy);
        userEntiy = updateUserInfo(item_info, userEntiy);
        log.info("更新完以后的数据为：" + userEntiy);
        userRepository.saveAndFlush(userEntiy);

        return userEntiy;
    }

    @Override
    public UserEntiy updateUserItemDown(BackDao backDao) {
        //根据item_id查询装备属性
        UserItemInfoEntity item_info = userItemInfoService.findItemInfoByItemId(backDao);
        //根据item_id修改背包里equip字段为1 表示穿戴
        BackInfoEntity newBack = backInfoRepository.findByItem_idAAndFlag(item_info.getItem_id(), 1);
        //卸下装备

        log.info("卸下装备");
        newBack.setEquip(0);
        backInfoRepository.saveAndFlush(newBack);
        UserItemInfoEntity userItemInfoEntity = userItemInfoService.findItemInfoByItemIdString(backDao.getItem_id());
        //根据账号查询信息进行计算
        UserEntiy userEntiy = userRepository.findByUser_account(backDao.getUser_account());
        log.info("卸下前的信息为：" + userEntiy);
        userEntiy = updateUserInfoJ(userItemInfoEntity, userEntiy);
        log.info("卸下后的信息为：" + userEntiy);
        userRepository.saveAndFlush(userEntiy);
        return userEntiy;

    }

    @Override
    public List<BackInfoEntity> deleteItem(BackDao backDao) {
        BackInfoEntity backInfoEntity = backInfoRepository.findByItem_idAAndFlag(backDao.getItem_id(), 1);
        backInfoEntity.setFlag(0);
        backInfoRepository.saveAndFlush(backInfoEntity);
        List<BackInfoEntity> backInfoEntities = backInfoRepository.findByUser_accountAndFlag(backDao.getUser_account(), 1);
        return backInfoEntities;
    }

    public UserEntiy updateUserInfo(UserItemInfoEntity item, UserEntiy userEntiy) {
        userEntiy.setUser_A(userEntiy.getUser_A() + item.getProperty_A());
        userEntiy.setUser_M(userEntiy.getUser_M() + item.getProperty_M());
        userEntiy.setUser_property(userEntiy.getUser_property() + item.getProperty_P());
        userEntiy.setUser_DF(userEntiy.getUser_DF() + item.getProperty_DF());
        userEntiy.setUser_HP(userEntiy.getUser_HP() + item.getProperty_HP());
        userEntiy.setUser_MP(userEntiy.getUser_MP() + item.getProperty_MP());
        return userEntiy;
    }

    public UserEntiy updateUserInfoJ(UserItemInfoEntity item, UserEntiy userEntiy) {
        userEntiy.setUser_A(userEntiy.getUser_A() - item.getProperty_A());
        userEntiy.setUser_M(userEntiy.getUser_M() - item.getProperty_M());
        userEntiy.setUser_property(userEntiy.getUser_property() - item.getProperty_P());
        userEntiy.setUser_DF(userEntiy.getUser_DF() - item.getProperty_DF());
        userEntiy.setUser_HP(userEntiy.getUser_HP() - item.getProperty_HP());
        userEntiy.setUser_MP(userEntiy.getUser_MP() - item.getProperty_MP());
        return userEntiy;
    }

}
